package com.llm.travel_agent_assistant.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.llm.travel_agent_assistant.dto.Result;
import com.llm.travel_agent_assistant.dto.TravelPlanDTO;
import com.llm.travel_agent_assistant.entity.TravelPlan;
import com.llm.travel_agent_assistant.mapper.TravelPlanMapper;
import com.llm.travel_agent_assistant.service.TravelPlanService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class TravelPlanServiceImpl implements TravelPlanService {

    @Resource
    private TravelPlanMapper travelPlanMapper;

    public Result getPlanByUserId(String userId) {
        QueryWrapper<TravelPlan> wrapper = new QueryWrapper<TravelPlan>()
                .eq("owner_id", userId)
                .orderByDesc("create_time");
        List<TravelPlan> list = travelPlanMapper.selectList(wrapper);
        return Result.ok(list);
    }

    @Override
    public Result updatePlan(TravelPlanDTO travelPlanDTO) {
        TravelPlan plan = new TravelPlan();
        BeanUtil.copyProperties(travelPlanDTO, plan);
        plan.setUpdateTime(LocalDateTime.now());
        int update = travelPlanMapper.update(
                plan,
                new QueryWrapper<TravelPlan>().eq("plan_id", plan.getPlanId())
        );
        if (update != 1) {
            return Result.fail("更新失败");
        }
        return Result.ok();
    }

    @Override
    public Result getPlanDetail(String planId) {
        QueryWrapper<TravelPlan> wrapper = new QueryWrapper<TravelPlan>()
                .eq("plan_id", planId);
        TravelPlan plan = travelPlanMapper.selectOne(wrapper);
        if (plan == null) {
            return Result.fail("旅行计划不存在");
        }
        return Result.ok(plan);
    }

    @Override
    public Result deletePlan(String planId) {
        QueryWrapper<TravelPlan> wrapper = new QueryWrapper<TravelPlan>()
                .eq("plan_id", planId);
        int delete = travelPlanMapper.delete(wrapper);
        if (delete != 1) {
            return Result.fail("删除失败，旅行计划不存在");
        }
        return Result.ok();
    }
}
